<template>
	<view class="fl-s-c p-r-30">
		<view class="w-1  m-t-20">
			<uni-swipe-action>
				<uni-swipe-action-item :right-options="options1" @click="delAddress(item.id,index)" v-for="(item,index) in addressList"
					:key="index">
					<view class="list-item w-1" @click="chooseAddress(item)">
						<view class="address_left">
							<view class="address_item_top">
								<view class="address_item_top_moren" v-if="item.default == 1">
									默认
								</view>
								<view class="address_item_top_qu">
									{{item.province}} {{item.city}} {{item.district}}
								</view>
							</view>
							<view class="address_item_min">
								{{item.province}} {{item.city}} {{item.district}} {{item.address}}
							</view>
							<view class="address_item_bottom">
								<text>{{item.name}}</text>
								<text class="address_item_bottom_right">{{item.mobile}}</text>
							</view>
						</view>
						<image src="/static/images/user/edit_address.png" @click.stop="toEditAddress(item)"
							class="address_right" mode=""></image>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</view>

		<!-- 提交按钮 -->
		<view class="iphonex fr-c" style="background: #f4f4f4;">
			<view class="fr-c sub-btn" @click="toAddress">添加地址</view>
		</view>
		<div style="margin-top: 250rpx;" v-if="nodata">
			<u-empty mode="address"></u-empty>
		</div>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				list: [],
				nodata: false,
				isChoose: 0,
				page: 1,
				current_page: '',
				status: '',
				total: '',
				last_page: '',
				addressList: [],
				options1: [{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				order_id: 0,
			}
		},
		onLoad(e) {
			if (e.isChoose) {
				this.isChoose = e.isChoose
			}
			if(e.order_id)this.order_id = e.order_id;
			this.loadData()
		},
		// onPullDownRefresh() {
		// 	this.getList(false)
		// },
		// 监听上拉加载
		onReachBottom() {
			//判断是否最后一页
			if (this.current_page >= this.last_page) {
				// this.status = 'noMore';
				uni.showToast({
					title: '没有更多了',
					icon: 'none'
				})
			} else {
				this.current_page = this.current_page + 1; //页码+1
				this.status = 'loading';
				this.loadData(true);
			}
		},
		
		methods: {
			//添加或修改成功之后回调
			async refreshList(data, type) {
				this.$api.post({
					url: '/wanlshop/address/address',
					data: {data: data,type: type},
					success: (res) => {
						this.loadData(false,true);
					}
				});
			},
			async loadData(reload, refresh) {
				this.$api.get({
					url: '/wanlshop/address/getaddress',
					loadingTip: refresh ? '更新列表中...' : '',
					data: {
						page: reload ? (this.current_page) : 1
					},
					success: res => {
						if(res.code == 1){
							for(let i in res.data.data){
								if(res.data.data[i].default == 1){
									uni.setStorageSync('setaddress',res.data.data[i])
								}
							}
						}
						if (reload) {
							this.addressList = this.addressList.concat(res.data.data);
						} else {
							this.addressList = res.data.data;
							uni.stopPullDownRefresh();
						}
						
						this.total = res.data.total; //数据量
						this.current_page = res.data.current_page; //当前页码
						this.last_page = res.data.last_page; //总页码
						this.status = res.total == 0 ? 'noMore' : 'more';
					}
				});
			},
			//添加或修改成功之后回调
			async refreshList(data, type) {
				this.$api.post({
					url: '/wanlshop/address/address',
					data: {
						data: data,
						type: type
					},
					success: (res) => {
						this.loadData(false, true);
					}
				});
			},
			chooseAddress(item) {
				if (this.isChoose == 1) {
					let pages = getCurrentPages()
					let lastPage = pages[pages.length - 2]
					lastPage.$vm.default_address = item
					uni.navigateBack()
				}else if(this.isChoose == 2){ //我的订单修改地址  地址ID   返回订单ID
					this.$wanlshop.prePage().refreshList(item.id, this.order_id);
					this.$wanlshop.back(1);
				}
			},
			Refresh() {
				this.getList()
			},
			toAddress() {
				uni.navigateTo({
					url: '/pages/UserCenter/MyAddress/AddAddress/AddAddress?type=add'
				})
			},
			// 编辑地址
			toEditAddress(item) {
				// uni.navigateTo({
				// 	url:'/pages/UserCenter/MyAddress/EditAddress/EditAddress?id='+id
				// })
				this.$wanlshop.to(
					`/pages/UserCenter/MyAddress/EditAddress/EditAddress?type=edit&data=${JSON.stringify(item)}`);
			},
			
			// 删除地址
			delAddress(id, index) {
				uni.showModal({
					title: '提示',
					content: '确认要删除该地址吗？',
					success: (res) => {
						if (res.confirm) {
							this.$post({
								url: '/wanlshop/address/deladdress',
								data: {
									id
								}
							}).then(res => {
								this.addressList.splice(index, 1)
								this.nodata = !this.list.length
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #f4f4f4;
		padding-bottom: 180rpx;
	}
	/deep/.uni-swipe{
		margin-bottom: 30rpx;
	}
	.list-item {
		/* height: 172rpx; */
		width: 100%;
		height: 100%;
		padding: 22rpx 30rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.sub-btn {
		width: 686rpx;
		height: 100rpx;
		background: linear-gradient(246deg, #FA360A 0%, #FA8C0A 100%);
		box-shadow: 0rpx 6rpx 32rpx 2rpx rgba(250, 74, 20, 0.58);
		border-radius: 50rpx;
		font-size: 32rpx;
		font-family: Alibaba;
		color: #FFFFFF;
		margin-bottom: 30rpx;
	}

	.address_item_top {
		display: flex;
		align-items: center;
	}

	.address_item_top_moren {
		width: 80rpx;
		height: 36rpx;
		background: #E32D1D;
		border-radius: 8rpx;
		font-size: 20rpx;
		font-family: Source Han Sans SC-Regular, Source Han Sans SC;
		font-weight: 400;
		color: #FFFFFF;
		text-align: center;
		line-height: 36rpx;
		margin-right: 12rpx;
	}

	.address_item_top_qu {
		font-size: 24rpx;
		font-family: Source Han Sans SC-Regular, Source Han Sans SC;
		font-weight: 400;
		color: #666666;
	}

	.address_right {
		min-width: 30rpx;
		width: 30rpx;
		height: 30rpx;
		margin-left: 20rpx;
	}

	.address_item_min {
		font-size: 30rpx;
		font-family: Source Han Sans SC-Medium, Source Han Sans SC;
		font-weight: 500;
		color: #333333;
		margin-top: 10rpx;
	}

	.address_item_bottom {
		font-size: 24rpx;
		font-family: Source Han Sans SC-Regular, Source Han Sans SC;
		font-weight: 400;
		color: #666666;
		margin-top: 10rpx;
	}

	.address_item_bottom_right {
		margin-left: 40rpx;
	}
</style>